package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.BusinessDao;
import com.wn.class30.mapper.ProductDao;
import com.wn.class30.mapper.ProducttypeDao;
import com.wn.class30.service.BackProductService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class BackProductServiceImpl implements BackProductService {
    @Resource
    private ProductDao productDao;
    @Resource
    private BusinessDao businessDao;
    @Resource
    private ProducttypeDao producttypeDao;
    @Override
    public List getAllBusiness() {
        return businessDao.selectByExample(null);
    }

    @Override
    public List getAllProTypes() {
        return producttypeDao.selectByExample(null);
    }

    @Override
    public int selectCount(HashMap map) {
        ProductExample ex = new ProductExample();
        String businessId = (String) map.get("businessId");
        String name = (String) map.get("name");
        String status = (String) map.get("status");
        String proCode = (String) map.get("proCode");
        String businessName = (String) map.get("businessName");
        String proType = (String) map.get("proType");
        ProductExample.Criteria criteria = ex.createCriteria();
        if(!"".equals(name) && name != null){
            criteria.andProductNameLike("%"+name+"%");
        }
        if(!"".equals(businessId) && businessId != null){
            criteria.andBusinessIdEqualTo(Integer.parseInt(businessId));
        }
        if(!"".equals(status) && status != null){
            criteria.andProductStatusEqualTo(Integer.parseInt(status));
        }
        if(!"".equals(proCode) && proCode != null){
            criteria.andProductCodeEqualTo(proCode);
        }
        if(!"".equals(businessName) && businessName != null){
            BusinessExample be = new BusinessExample();
            BusinessExample.Criteria criteria1 = be.createCriteria();
            criteria1.andBusinessNameEqualTo(businessName);
            List<Business> business = businessDao.selectByExample(be);
            criteria.andBusinessIdEqualTo(business.get(0).getBusinessId());
        }
        if(!"".equals(proType) && proType != null){
            ProducttypeExample pe = new ProducttypeExample();
            ProducttypeExample.Criteria criteria1 = pe.createCriteria();
            criteria1.andProducttypeNameEqualTo(proType);
            List<Producttype> producttypes = producttypeDao.selectByExample(pe);
            criteria.andProductTypeEqualTo(producttypes.get(0).getProducttypeId());
        }
        return (int) productDao.countByExample(ex);
    }

    @Override
    public Object select(HashMap map) {
        System.out.println("vccvx");
        ProductExample ex = new ProductExample();
        String businessId = (String) map.get("businessId");
        String name = (String) map.get("name");
        String status = (String) map.get("status");
        String proCode = (String) map.get("proCode");
        String businessName = (String) map.get("businessName");
        String proType = (String) map.get("proType");
        Integer page = 0;
        if(!"".equals((String) map.get("page"))&&(String) map.get("page")!=null){
            page = Integer.parseInt((String) map.get("page"));
        }
        Integer limit = 0;
        if(!"".equals((String) map.get("limit"))&&(String) map.get("limit")!=null){
            limit = Integer.parseInt((String) map.get("limit"));
        }
        ex.setOffset(((long)(page-1))*limit);
        ex.setLimit(limit);
        ProductExample.Criteria criteria = ex.createCriteria();
        if(!"".equals(businessId) && businessId != null){
            criteria.andBusinessIdEqualTo(Integer.parseInt(businessId));
        }
        if(!"".equals(name) && name != null){
            criteria.andProductNameLike("%"+name+"%");
        }
        if(!"".equals(status) && status != null){
            criteria.andProductStatusEqualTo(Integer.parseInt(status));
        }
        if(!"".equals(proCode) && proCode != null){
            criteria.andProductCodeEqualTo(proCode);
        }
        if(!"".equals(businessName) && businessName != null){
            BusinessExample be = new BusinessExample();
            BusinessExample.Criteria criteria1 = be.createCriteria();
            criteria1.andBusinessNameEqualTo(businessName);
            List<Business> business = businessDao.selectByExample(be);
            criteria.andBusinessIdEqualTo(business.get(0).getBusinessId());
        }
        if(!"".equals(proType) && proType != null){
            ProducttypeExample pe = new ProducttypeExample();
            ProducttypeExample.Criteria criteria1 = pe.createCriteria();
            criteria1.andProducttypeNameEqualTo(proType);
            List<Producttype> producttypes = producttypeDao.selectByExample(pe);
            criteria.andProductTypeEqualTo(producttypes.get(0).getProducttypeId());
        }
        List<Product> products = productDao.selectByExample(ex);
        List<Product> products1 = new ArrayList<>();
        //给商品封装对应的商家和类型对象
        for (Product product : products) {
            product.setBusiness(businessDao.selectByPrimaryKey(product.getBusinessId()));
            product.setProducttype(producttypeDao.selectByPrimaryKey(product.getProductType()));
            products1.add(product);
        }

        return products1;
    }

    @Override
    public void deleteProductById(Integer integer) {
        productDao.deleteByPrimaryKey(integer);
    }

    @Override
    public Product selectProductById(Integer productId) throws Exception {
        return productDao.selectByPrimaryKey(productId);
    }

    @Override
    public void updateProductById(Product product) throws Exception {
        productDao.updateByPrimaryKey(product);
    }

    @Override
    public Integer insertProduct(Product product) throws Exception {
        productDao.insertSelective(product);
        return product.getProductId();
    }
}
